/*
* $Id: AddPointView.cpp 153 2010-09-26 17:33:07Z volok-aleksej@yandex.ru $
*
* Copyright 2009 by Volokitin Alexey
* All rights reserved
*
*/
#include "Common/include.h"
#include "AddPointView.h"
#include "ViewFieldValuesHelper.h"
#include "Visual/Drawers/EditorDrawer/HeaderItem.h"
#include "Visual/Drawers/EditorDrawer/EditItem.h"
#include "Settings/Journal/PointViewSettings.h"
#include "Settings/DinIntDefined.h"
#include "Settings/SettingControlIds.h"
#include "Components/Journal/FamilyManager.h"

#pragma warning(disable: 4407)

#define TITLE_ID		1
#define NAME_POINT_ID	2
#define NOTE_POINT_ID	3
#define USER_FIELD_ID	4

CAddPointView::CAddPointView(CState* pState)
: CAddChangeView(pState), m_nUserFields(0){}
CAddPointView::~CAddPointView(){}

void CAddPointView::Init(){
	m_editorDrawer.Clear();
	m_editorDrawer.AddItem(new CHeaderItem(ILanguageManager::GetLanguageManager()->GetString(ADD_POINT_ID), TITLE_ID));
	m_editorDrawer.AddItem(new CEditItem(ILanguageManager::GetLanguageManager()->GetString(TABLE_HEADER_POINT_NAME_ID), NAME_POINT_ID, editItem::TEXT));
	CPointViewSettings pSettings;
	if(pSettings.GetState(CHECK_NOTE_ID) == CHECK_BOX_STATE_1)
		m_editorDrawer.AddItem(new CEditItem(ILanguageManager::GetLanguageManager()->GetString(TABLE_HEADER_NOTE_ID), NOTE_POINT_ID, editItem::TEXT));
	CViewFieldValuesHelper helper(&m_editorDrawer);
	m_nUserFields = helper.GenerateUserEditItems(TypeField::POINTS, USER_FIELD_ID, 0);
}

void CAddPointView::SetState(){
	m_pState->m_nState = Journal::VIEW_ADD_POINT;
	m_pState->m_nIdParameter = 0;
}
bool CAddPointView::IsViewState(State state){
	return state.m_type == Journal::ADD_POINT;
}

BEGIN_MESSAGE_MAP(CAddPointView, CAddChangeView)
	ON_COMMAND(BUTTON_SAVE_ID, OnSavePoint)
END_MESSAGE_MAP()

void CAddPointView::OnSavePoint(){
	CFamilyManager* pManager = IJournalComponent::GetFamilyManager();
	CFamilyManager::point pnt;
	pnt.m_strName = m_editorDrawer.GetValueItem(NAME_POINT_ID);
	pnt.m_strNote = m_editorDrawer.GetValueItem(NOTE_POINT_ID);
	std::map<std::tstring, std::tstring> aUserFields;
	for(int i = 1; i <= m_nUserFields; i++)
		aUserFields.insert(std::make_pair(m_editorDrawer.GetNameItem(USER_FIELD_ID + i), m_editorDrawer.GetValueItem(USER_FIELD_ID + i)));
	pManager->AddPoint(pnt, aUserFields);
	__super::OnCancel();
}